package com.example.areaofthesquare;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.List;

/**
 * 绘制最终结果的自定义View
 */
public class CustomView extends View {

    //放大尺寸
    private float size = 50f;
    //所有点的坐标
    private List<Point> mAllPointList;
    //正方形四个角的点的坐标
    private List<Point> mSquarePointList;
    //宽度和高度
    private int mHeight;
    private int mWidth;
    //绘制所有点的画笔
    private Paint mAllPointPaint;
    //绘制正方形的画笔
    private Paint mSquarePaint;
    //坐标系的画笔
    private Paint mXYPaint;
    //圆心坐标
    private float mCenterX;
    private float mCenterY;

    private void initPaint(){
        //画所有点
        mAllPointPaint = new Paint();
        mAllPointPaint.setAntiAlias(true);          //抗锯齿
        mAllPointPaint.setColor(getResources().getColor(R.color.black));//画笔颜色
        mAllPointPaint.setStyle(Paint.Style.FILL);  //画笔风格
        mAllPointPaint.setTextSize(10);             //绘制文字大小，单位px
        mAllPointPaint.setTextAlign(Paint.Align.CENTER);
        mAllPointPaint.setStrokeWidth(3);           //画笔粗细
        //画正方形
        mSquarePaint = new Paint();
        mSquarePaint.setAntiAlias(true);          //抗锯齿
        mSquarePaint.setColor(getResources().getColor(R.color.blue));//画笔颜色
        mSquarePaint.setStyle(Paint.Style.STROKE);  //画笔风格
//        mSquarePaint.setTextAlign(Paint.Align.CENTER);
//        mSquarePaint.setTextSize(36);             //绘制文字大小，单位px
        mSquarePaint.setStrokeWidth(5);           //画笔粗细
        mSquarePaint.setStrokeCap(Paint.Cap.ROUND);//设置边角为圆弧
        //画坐标系
        mXYPaint = new Paint();
        mXYPaint.setAntiAlias(true);          //抗锯齿
        mXYPaint.setColor(getResources().getColor(R.color.black));//画笔颜色
        mXYPaint.setStyle(Paint.Style.STROKE);  //画笔风格
        mXYPaint.setTextSize(36);             //绘制文字大小，单位px
        mXYPaint.setStrokeWidth(5);           //画笔粗细
    }


    public CustomView(Context context) {
        super(context);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mHeight = getMeasuredHeight();
        mWidth = getMeasuredWidth();
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    public void setmAllPointList(List<Point> mAllPointList) {
        this.mAllPointList = mAllPointList;
    }

    public void setmSquarePointList(List<Point> mSquarePointList) {
        this.mSquarePointList = mSquarePointList;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initPaint();
        //先确定缩放和放大比例
        float maxX = 0;
        float maxY = 0;
        for (int i = 0; i < mAllPointList.size(); i++) {
            if (mAllPointList.get(i).getX() > maxX) {
                maxX = mAllPointList.get(i).getX();
            }
            if (mAllPointList.get(i).getY() > maxY) {
                maxY = mAllPointList.get(i).getY();
            }
        }

        float sizeX = (mWidth/2f - 100) / maxX;
        float sizeY = (mHeight/2f - 100) / maxY;
        size = Math.min(sizeX, sizeY);
        if (size > 50f) {
            size = 50f;
        }
        Log.i("Minsize",size+","+sizeX+","+sizeY);

        //先画坐标系
        mCenterX = mWidth / 2f;
        mCenterY = mHeight / 2f;

        //Y轴
        Path yPath = new Path();
        yPath.moveTo(mCenterX,mHeight);
        yPath.lineTo(mCenterX,0);
        canvas.drawPath(yPath,mXYPaint);
        yPath.moveTo(mCenterX-20,50);
        yPath.lineTo(mCenterX,0);
        canvas.drawPath(yPath,mXYPaint);
        yPath.moveTo(mCenterX+20,50);
        yPath.lineTo(mCenterX,0);
        canvas.drawPath(yPath,mXYPaint);
        //X轴
        Path xPath = new Path();
        xPath.moveTo(mWidth,mCenterY);
        xPath.lineTo(0,mCenterY);
        canvas.drawPath(xPath,mXYPaint);
        xPath.moveTo(mWidth-50,mCenterY-20);
        xPath.lineTo(mWidth,mCenterY);
        canvas.drawPath(xPath,mXYPaint);
        xPath.moveTo(mWidth-50,mCenterY+20);
        xPath.lineTo(mWidth,mCenterY);
        canvas.drawPath(xPath,mXYPaint);
        //根据坐标画正方形
        Path squarePath = new Path();
        float pointX = toSize("X",mSquarePointList.get(0).getX());
        float pointY = toSize("Y",mSquarePointList.get(0).getY());
        squarePath.moveTo(pointX,pointY);
        for (int i = 1; i < mSquarePointList.size(); i++) {
            pointX = toSize("X",mSquarePointList.get(i).getX());
            pointY = toSize("Y",mSquarePointList.get(i).getY());
            squarePath.lineTo(pointX,pointY);
            canvas.drawPath(squarePath, mSquarePaint);
            squarePath.moveTo(pointX,pointY);
            if (i == 3) {
                squarePath.lineTo(toSize("X",mSquarePointList.get(0).getX()),toSize("Y",mSquarePointList.get(0).getY()));
                canvas.drawPath(squarePath, mSquarePaint);
            }
        }
        squarePath.moveTo(toSize("X",mSquarePointList.get(0).getX()),toSize("Y",mSquarePointList.get(0).getY()));
        squarePath.lineTo(toSize("X",mSquarePointList.get(2).getX()),toSize("Y",mSquarePointList.get(2).getY()));
        canvas.drawPath(squarePath, mSquarePaint);
        //根据点坐标画点
        for (int i = 0; i < mAllPointList.size(); i++) {
             pointX = toSize("X",mAllPointList.get(i).getX());
             pointY = toSize("Y",mAllPointList.get(i).getY());

            canvas.drawCircle(pointX,pointY,5f,mAllPointPaint);
            mAllPointPaint.setStrokeWidth(2);
            mAllPointPaint.setTextSize(20);
            mAllPointPaint.setStyle(Paint.Style.FILL);
            float textX = Utils.decimalSize(mAllPointList.get(i).getX());
            float textY = Utils.decimalSize(mAllPointList.get(i).getY());
            canvas.drawText("("+ textX+","+ textY +")",pointX,pointY-15,mAllPointPaint);
        }
    }

    /**
     * 根据点的坐标转换成图中的位置
     * @param pointParam
     * @return
     */
    private float toSize(String centerType,float pointParam){

        if (centerType == "X") {
            return mCenterX + pointParam * size;
        }
        if (centerType == "Y") {
            return mCenterY - pointParam * size;
        }
        return 0;
    }
}
